<?php
/**
 * Created by PhpStorm.
 * User: DD
 * Date: 2016/4/18
 * Time: 13:55
 */

namespace app\common\services;


use think\Session;

class LoginService
{
    private static $_sessionKey = '';
    private static $_exceptedControllers = ['login'];
    private static $_redirectLoginUrl = '/login';
    private static $_exceptUrls = [];
    private static $_autoCheck = true;

    public static function registerSessionKey($key)
    {
        self::$_sessionKey = $key;
    }

    public static function registerExceptControllers($controller)
    {
        if (is_string($controller)) {
            $controller = [$controller];
        }
        self::$_exceptedControllers = $controller;
    }

    public static function registerExceptUrls($urls)
    {
        self::$_exceptUrls = $urls;
    }

    public static function registerLoginUrl($url)
    {
        self::$_redirectLoginUrl = $url;
    }

    public static function getLoginUserId()
    {
        return self::getLoginUser('id');
    }

    public static function getLoginUser($field = null, $default = null)
    {
        $user = Session::get(self::$_sessionKey);
        if (empty($user)) {
            if ($field == 'id') {
                self::redirectToLogin();
                exit;
            }
            return $default;
        }
        if (!empty($field)) {
            return $user[$field] ?: $default;
        }
        return $user;
    }

    public static function isLoginOn()
    {
        $user = Session::get(self::$_sessionKey);
        if (empty($user)) {
            return false;
        }
        return true;
    }

    public static function logOff()
    {
        Session::delete(self::$_sessionKey);
    }

    public static function getLoginUserName()
    {
        return self::getLoginUser('username') ?: 'system';
    }

    public static function loginOn($user)
    {
        Session::set('user', $user);
    }

    public static function isSuperMan()
    {
        return self::getLoginUserName() == 'admin';
    }

    public static function setAutoCheck($autoCheck)
    {
        self::$_autoCheck = $autoCheck;
    }

    public static function checkLogin()
    {
        if (self::$_autoCheck) {
            if (!in_array(CONTROLLER_NAME, self::$_exceptedControllers)) {
                if (!in_array(CONTROLLER_NAME . '/' . ACTION_NAME, self::$_exceptUrls)) {
                    if (!self::isLoginOn()) {
                        self::redirectToLogin();
                        exit();
                    }
                }
            }
        } else {
            if (!self::isLoginOn()) {
                self::redirectToLogin();
                exit();
            }
        }
    }

    public static function redirectToLogin()
    {
        if ($_SERVER['REQUEST_URI'] != self::$_redirectLoginUrl) {
            if (strpos(self::$_redirectLoginUrl, '?') !== false) {
                header("location:" . self::$_redirectLoginUrl . '&returnUrl=' . urlencode(ccnn_request_url()));
            } else {
                header("location:" . self::$_redirectLoginUrl . '?returnUrl=' . urlencode(ccnn_request_url()));
            }

        }
    }

    public static function setLoginUser($user)
    {
        Session::set(self::$_sessionKey, $user);
    }
}